package com.itcast.activiti.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.activiti.domain.AddSign;
import com.itcast.activiti.entity.ApproveAddSign;
import com.itcast.activiti.mapper.ApproveAddSignMapper;
import com.itcast.activiti.service.IApproveAddSignService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2021-12-21
 */
@Service
public class ApproveAddSignServiceImpl extends ServiceImpl<ApproveAddSignMapper, ApproveAddSign> implements IApproveAddSignService {
    @Override
    public void add(AddSign addSign, String addSignId, String taskDefinitionKey, String processDefinitionId, String newProcessDefinitionId, String assignee) {
        ApproveAddSign approveAddSign = new ApproveAddSign();
        approveAddSign.setId(addSignId);
        approveAddSign.setType(addSign.getType());
        approveAddSign.setUserEmails(JSONArray.toJSONString(addSign.getUserEmails()));
        approveAddSign.setMultiApproveMode(addSign.getMultiApproveMode());
        approveAddSign.setTargetNodeId(taskDefinitionKey);
        approveAddSign.setProcDefIdOld(processDefinitionId);
        approveAddSign.setProcDefId(newProcessDefinitionId);
        approveAddSign.setState(Boolean.TRUE);
        approveAddSign.setCreatedBy(assignee);
        approveAddSign.setCreatedDatetime(LocalDateTime.now());
        baseMapper.insert(approveAddSign);
    }

    @Override
    public List<ApproveAddSign> findByProcDefId(String procDefId) {
        return baseMapper.selectList(Wrappers.<ApproveAddSign>lambdaQuery().eq(ApproveAddSign::getProcDefId, procDefId));
    }

    @Override
    public List<ApproveAddSign> findByProcDefIdOlds(List<String> processDefIds) {
        return baseMapper.selectList(Wrappers.<ApproveAddSign>lambdaQuery().in(ApproveAddSign::getProcDefIdOld, processDefIds));
    }

    @Override
    public ApproveAddSign findById(String addSignId) {
        return baseMapper.selectOne(Wrappers.<ApproveAddSign>lambdaQuery().eq(ApproveAddSign::getId, addSignId));
    }
}
